$(function () {
        //抽取变量
        var clientTrace_datagrid = $("#clientTrace_datagrid");
        var clientTraceAdd_dialog = $("#clientTraceAdd_dialog");
        var clientTraceAdd_form = $("#clientTraceAdd_form");
        var audit_dialog = $("#audit_dialog");
        var audit_form = $("#audit_form");
        //初始化datagrid
        clientTrace_datagrid.datagrid({
            fit: true,
            fitColumns: true,
            striped: true,
            url: "/clientTrace/list.do",
            pagination: true,
            rownumbers: true,
            singleSelect: true,
            toolbar: "#clientTrace_toolbar",
            columns: [[
                {
                    field: 'client', title: '客户', width: 60, align: 'center', formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                }, {
                    field: 'inputUser',
                    title: '咨询人员',
                    width: 60,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return value ? value.username : "";
                    }
                },
                {field: 'lastTraceTime', title: '本次跟进时间', width: 220, align: 'center'},
                {field: 'nextTraceTime', title: '下次跟进时间', width: 220, align: 'center'},
                {
                    field: 'tracePurpose', title: '跟进目的', width: 80, align: 'center',
                    formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {
                    field: 'communicationMethod', title: '咨询方式', width: 80, align: 'center',
                    formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {field: 'qq', title: 'QQ', width: 80, align: 'center'},
                {field: 'tel', title: '电话', width: 80, align: 'center'},
                {
                    field: 'intentionDegree',
                    title: '意向程度',
                    width: 100,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {field: 'explaination', title: '审核说明', width: 100, align: 'center'},
                {
                    field: 'audited',
                    title: '审核状态',
                    width: 60,
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value) {
                            return "<span style='color: red'>已审核</span>";
                        } else {
                            return "未审核";
                        }
                    }
                }
            ]]
        });
        //初始化dialog
        clientTraceAdd_dialog.dialog({
            width: 700,
            height: 500,
            buttons: "#client_btns",
            closed: true
        });
        //初始化审核弹窗dialog
        audit_dialog.dialog({
            width: 700,
            height: 500,
            buttons: "#audit_btns",
            closed: true
        });
        //所有ab标签的方法都交给methodObject来管理
        var methodObjdect = {
            //编辑
            edit: function () {
                clientTraceAdd_form.form("clear");
                $("#client_btns").show();
                $(".easyui-combobox").combo("readonly", false);
                //获取选中行的数据
                var row = clientTrace_datagrid.datagrid("getSelected");
                console.log(row);
                if (!row) {
                    $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
                } else {
                    row["id"] = row.id;
                    if (row.client) {
                        row["client.id"] = row.client.id;
                    }
                    if (row.state) {
                        row["state.id"] = row.state.id;
                    }
                    if (row.saleman) {
                        row["saleman.id"] = row.saleman.id;
                    }
                    if (row.university) {
                        row["university.id"] = row.university.id;
                    }
                    if (row.clz) {
                        row["clz.id"] = row.clz.id;
                    }
                    if (row.communicationMethod) {
                        row["communicationMethod.id"] = row.communicationMethod.id;
                    }
                    if (row.intentionDegree) {
                        row["intentionDegree.id"] = row.intentionDegree.id;
                    }
                    if (row.tracePurpose) {
                        row["tracePurpose.id"] = row.tracePurpose.id;
                    }
                    //将查询的数据加载进编辑表单中
                    clientTraceAdd_form.form("load", row);
                    clientTraceAdd_dialog.dialog("setTitle", "跟踪记录编辑");
                    clientTraceAdd_dialog.dialog("open");
                }
            },
            //查看
            view: function () {
                clientTraceAdd_form.form("clear");
                //获取选中行的数据
                var row = clientTrace_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
                } else {
                    if (row.client) {
                        row["client.id"] = row.client.id;
                    }
                    if (row.state) {
                        row["state.id"] = row.state.id;
                    }
                    if (row.saleman) {
                        row["saleman.id"] = row.saleman.id;
                    }
                    if (row.university) {
                        row["university.id"] = row.university.id;
                    }
                    if (row.clz) {
                        row["clz.id"] = row.clz.id;
                    }
                    if (row.communicationMethod) {
                        row["communicationMethod.id"] = row.communicationMethod.id;
                    }
                    if (row.intentionDegree) {
                        row["intentionDegree.id"] = row.intentionDegree.id;
                    }
                    if (row.tracePurpose) {
                        row["tracePurpose.id"] = row.tracePurpose.id;
                    }
                    //将查询的数据加载进编辑表单中
                    $("#client_btns").hide();
                    clientTraceAdd_form.form("load", row);
                    $(".easyui-combobox").combo("readonly", true);
                    clientTraceAdd_dialog.dialog("setTitle", "跟踪记录编辑");
                    $("input").prop("disabled", true);
                    clientTraceAdd_dialog.dialog("open");
                }
            },
            //新增/编辑保存操作
            save: function () {
                clientTraceAdd_form.form("submit", {
                    url: "/clientTrace/saveOrUpdate.do",
                    success: function (data) {
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, 'info', function () {
                                clientTraceAdd_dialog.dialog("close");
                                clientTrace_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert("温馨提示", data.msg, 'info', function () {
                                clientTraceAdd_dialog.dialog("close");
                            })
                        }
                    }
                })
            },
            //刷新操作
            reload: function () {
                clientTrace_datagrid.datagrid("reload");
            },
            //审核操作
            audit: function () {
                audit_form.form("clear");
                $(".easyui-combobox").combo("readonly", false);
                $("#inputUser").combo("readonly", true);
                //获取选中行的数据
                var row = clientTrace_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
                } else {
                    if (row.client) {
                        row["client.name"] = row.client.name;
                    }
                    if (row.inputUser) {
                        row["inputUser.id"] = row.inputUser.id;
                    }
                    if (row.communicationMethod) {
                        row["communicationMethod.name"] = row.communicationMethod.name;
                    }
                    if (row.score) {
                        row["score.id"] = row.score.id;
                    }
                    //将查询的数据加载进编辑表单中
                    audit_form.form("load", row);
                    audit_dialog.dialog("setTitle", "跟踪记录审核");
                    audit_dialog.dialog("open");
                }
            },
            //审核保存操作
            auditSubmit: function () {
                audit_form.form("submit", {
                    url: "/clientTrace/audit.do",
                    success: function (data) {
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, 'info', function () {
                                audit_dialog.dialog("close");
                                clientTrace_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert("温馨提示", data.msg, 'info', function () {
                                audit_dialog.dialog("close");
                            })
                        }
                    }
                })
            },
            //取消
            cancel: function () {
                clientTraceAdd_dialog.dialog("close");
            },
            //取消审核
            cancelAudit: function () {
                audit_dialog.dialog("close");
            },
        };
        //methodObject调用方法
        $("[data-cmd]").click(function () {
            var cmd = $(this).data("cmd");
            methodObjdect[cmd]();
        })
    }
);